home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0065_BP Bug.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  2KB  |  80 lines

  1. {
  2. I'm not sure if the following bug in Contains() of STDDLG.PAS has been fixed
  3. in 7.01 (since I still don't have it) so I decided to post it.
  4.  
  5. STDDLG.PAS, function Contains()
  6. }
  7. { Contains returns true if S1 contains any characters in S2 }
  8. function Contains(S1, S2 : String): Boolean; near; assembler;
  9. asm
  10.   PUSH    DS
  11.   CLD
  12.   LDS     SI, S1
  13.   LES     DI, S2
  14.   MOV     DX, DI
  15. > INC     DX           { DX still pointed at len byte }
  16.   XOR     AH, AH
  17.   LODSB
  18.   MOV     BX, AX
  19.   OR      BX, BX
  20.   JZ      @@2
  21.   MOV     AL, ES:[DI]
  22.   XCHG    AX, CX
  23.  @@1:
  24.   PUSH    CX
  25.   MOV     DI, DX
  26.   LODSB
  27.   REPNE   SCASB
  28.   POP     CX
  29.   JE      @@3
  30.   DEC     BX
  31.   JNZ     @@1
  32.  @@2:
  33.   XOR     AL, AL
  34.   JMP     @@4
  35.  @@3:
  36.   MOV     AL, 1
  37.  @@4:
  38.   POP     DS
  39. end;
  40.  
  41. {
  42. BUT: fixing the bug reveals another bug  <g>
  43.  
  44. The function is used to determine whether a filename or path contains illegal
  45. characters or not. The last character in the constant "IllegalChars" is the
  46. backslash "\" that would have been ignored by the buggy version of Contains().
  47. However, the corrected version returns TRUE for Contains('\MYPATH\',
  48. IllegalChars) (as it's supposed to).  Since a path name created by FSplit
  49. normally contains a "\" the filename is considered as FALSE by ValidFileName.
  50. My solution is to add a second const named IllegalCharsFN for illegal chars in
  51. the filename (but legal chars in path names) currently just containing '\'.
  52. Furthermore, I removed space ' ' from the list of illegal characters (since it
  53. isn't an illegal char!) and added '/' instead. But have a look at my final
  54. correction suggestion:
  55. }
  56.  
  57. function ValidFileName(var FileName : PathStr) : Boolean;
  58. const
  59.   IllegalCharsFN = '\';
  60.   IllegalChars   = ';,=+<>|"[]/';
  61. var
  62.   Dir  : DirStr;
  63.   Name : NameStr;
  64.   Ext  : ExtStr;
  65.  
  66.   { Contains returns true if S1 contains any characters in S2 }
  67.   function Contains(S1, S2 : String) : Boolean; near; assembler;
  68.   asm
  69.      {...see above...}
  70.   end;
  71.  
  72. begin
  73.   ValidFileName := True;
  74.   FSplit(FileName, Dir, Name, Ext);
  75.   if not ((Dir = '') or PathValid(Dir)) or
  76.      Contains(Name, IllegalChars + IllegalCharsFN) or
  77.      Contains(Dir, IllegalChars) then
  78.     ValidFileName := False;
  79. end;
  80.